<?php
namespace Home\Controller;

use Common\Controller\HomeController;

class UserController extends HomeController
{
    /**
     * 用户列表
     * @author Mr.Alien
     * @return string
     */
    public function index($p = 1)
    {
        I('get.name') && ($this->where['name'] = I('get.name'));
        I('get.phone') && ($this->where['phone'] = array('like','%'.I('get.phone').'%'));
        parent::index($p);
    }

    /**
     * 添加用户信息
     * @author Mr.Alien
     * @return string
     */
    public function add()
    {
        $group = M("AuthGroup")->select();
        $this->assign('group',$group);
        parent::add();
    }

    /**
     * 修改用户信息
     * @author Mr.Alien
     * @param  $id int
     * @return string
     */
    public function edit($id)
    {
        $group = M("AuthGroup")->where(array('status'=>1))->select();
        $this->assign('group',$group);
        parent::edit($id);
    }

    /**
     * 保存用户信息
     * @author Mr.Alien
     * @return string
     */
    public function save()
    {
        $data = I('post.');
        if(I("post.pass")){
            $data['pass'] = md5($data['pass']);
        }else{
            unset($data['pass']);
        }
        if($data['id']){
            $res = M(CONTROLLER_NAME)->save($data);
            M("AuthGroupAccess")->where(array('uid'=>$data['id']))->delete();
            I("post.group_id") && M("AuthGroupAccess")->add(array('uid'=>$data['id'],'group_id'=>$data['group_id']));
        }else{
            $res = M(CONTROLLER_NAME)->add($data);
            M("AuthGroupAccess")->where(array('uid'=>$res))->delete();
            I("post.group_id") && M("AuthGroupAccess")->add(array('uid'=>$res,'group_id'=>$data['group_id']));
        }
        if($res === false){
            $this->error('保存失败');
        }else{
            $this->success('保存成功');
        }
    }

    /**
     * 禁用用户
     * @author Mr.Alien
     */
    public function forbidden()
    {
        $data = I('post.');
        $res = M('User')->where(array('id'=>$data['id']))->save(array('status'=>$data['status']));
        if($res === false){
            $this->error('保存失败');
        }else{
            $this->success('保存成功');
        }
    }

}